<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="模板id" prop="formworkid">
      <el-input v-model="dataForm.formworkid" placeholder="模板id"></el-input>
    </el-form-item>
    <el-form-item label="模板类型" prop="formworktype">
      <el-input v-model="dataForm.formworktype" placeholder="模板类型"></el-input>
    </el-form-item>
    <el-form-item label="模板标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="模板标题"></el-input>
    </el-form-item>
    <el-form-item label="模板附标题" prop="subtitle">
      <el-input v-model="dataForm.subtitle" placeholder="模板附标题"></el-input>
    </el-form-item>
    <el-form-item label="模板icon" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="模板icon"></el-input>
    </el-form-item>
    <el-form-item label="使用次数" prop="usecount">
      <el-input v-model="dataForm.usecount" placeholder="使用次数"></el-input>
    </el-form-item>
    <el-form-item label="最大输入次数" prop="maxcount">
      <el-input v-model="dataForm.maxcount" placeholder="最大输入次数"></el-input>
    </el-form-item>
    <el-form-item label="关键字" prop="keyword">
      <el-input v-model="dataForm.keyword" placeholder="关键字"></el-input>
    </el-form-item>
    <el-form-item label="占位符" prop="placeholder">
      <el-input v-model="dataForm.placeholder" placeholder="占位符"></el-input>
    </el-form-item>
    <el-form-item label="按钮名称" prop="btnname">
      <el-input v-model="dataForm.btnname" placeholder="按钮名称"></el-input>
    </el-form-item>
    <el-form-item label="系统内容" prop="systemcontent">
      <el-input v-model="dataForm.systemcontent" placeholder="系统内容"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          formworkid: '',
          formworktype: '',
          title: '',
          subtitle: '',
          icon: '',
          usecount: '',
          maxcount: '',
          keyword: '',
          placeholder: '',
          btnname: '',
          systemcontent: ''
        },
        dataRule: {
          formworkid: [
            { required: true, message: '模板id不能为空', trigger: 'blur' }
          ],
          formworktype: [
            { required: true, message: '模板类型不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '模板标题不能为空', trigger: 'blur' }
          ],
          subtitle: [
            { required: true, message: '模板附标题不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '模板icon不能为空', trigger: 'blur' }
          ],
          usecount: [
            { required: true, message: '使用次数不能为空', trigger: 'blur' }
          ],
          maxcount: [
            { required: true, message: '最大输入次数不能为空', trigger: 'blur' }
          ],
          keyword: [
            { required: true, message: '关键字不能为空', trigger: 'blur' }
          ],
          placeholder: [
            { required: true, message: '占位符不能为空', trigger: 'blur' }
          ],
          btnname: [
            { required: true, message: '按钮名称不能为空', trigger: 'blur' }
          ],
          systemcontent: [
            { required: true, message: '系统内容不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/config/configformwork/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.formworkid = data.configFormwork.formworkid
                this.dataForm.formworktype = data.configFormwork.formworktype
                this.dataForm.title = data.configFormwork.title
                this.dataForm.subtitle = data.configFormwork.subtitle
                this.dataForm.icon = data.configFormwork.icon
                this.dataForm.usecount = data.configFormwork.usecount
                this.dataForm.maxcount = data.configFormwork.maxcount
                this.dataForm.keyword = data.configFormwork.keyword
                this.dataForm.placeholder = data.configFormwork.placeholder
                this.dataForm.btnname = data.configFormwork.btnname
                this.dataForm.systemcontent = data.configFormwork.systemcontent
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/config/configformwork/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'formworkid': this.dataForm.formworkid,
                'formworktype': this.dataForm.formworktype,
                'title': this.dataForm.title,
                'subtitle': this.dataForm.subtitle,
                'icon': this.dataForm.icon,
                'usecount': this.dataForm.usecount,
                'maxcount': this.dataForm.maxcount,
                'keyword': this.dataForm.keyword,
                'placeholder': this.dataForm.placeholder,
                'btnname': this.dataForm.btnname,
                'systemcontent': this.dataForm.systemcontent
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
